<template>
  <div class="tabbar">
      <div class="wrap tabbarwrap">
        <div class="left">欢迎来到叩丁狼积分商城</div>
        <ul class="right">   
          <li><img :src="userInfo.headImg" width="24px" alt=""> 用户名：{{userInfo.nickName}}</li>
          <li>我的积分：{{userInfo.coin}}</li>
          <li>获取积分</li>
          <li>叩丁狼官网</li>
          

          <li class="cart-btn" v-if="isLogined">
            <img src="../assets/img/cart.png" alt="">
            <span>购物车</span>
            <strong>{{cartTotal}}</strong>
          </li><li class="btnlogin" @click="changeIsShowLoginModal(true)" v-else>登录</li>
          
        </ul>
      </div>
  </div>
</template>

<script>
import { mapState,mapMutations, mapActions} from "vuex"
import { WeiXinLoginAPI } from "../request/api"
export default {
  async created(){
    console.log("topbar-created..",this.$route.query.code);
    let mycode = this.$route.query.code
    if(mycode){
      let res = await WeiXinLoginAPI({
        code:mycode
      });
      if(res.code == 0){
        alert("扫码登录成功")
        localStorage.setItem("x-auth-token",res["x-auth-token"])
        this.$router.push(this.$route.path)
        this.changeIsLogined(true)
        this.asyncChangeUserInfo()
      }else if(res.code == 400){
        alert("请重新扫码")
        this.changeIsShowLoginModal(true)
      }else if(res.code == 407){
        // 1.提示用户需要手机号再登录一次
      alert("请使用手机号在登录一下")
      // 2.帮用户打开登录窗口
      this.changeIsShowLoginModal(true)
      // 3.保存uuid
      localStorage.setItem("uuid",res.uuid)
    }
    // console.log("扫码结果：",res);
    } else{
      console.log("没有保存就进来了");
      let mytoken = localStorage.getItem("x-auth-token")
      this.changeIsLogined(Boolean(mytoken))
      if(mytoken){
        this.asyncChangeUserInfo()
      }else{
        console.log("初始化");
        this.initUserInfo()
      }
    }
  },
  watch:{
    // "$route.path":{
    //   handler(newVal,oldVal){
    //     console.log("地址变化了",newVal,oldVal);
    //     let mytoken = localStorage.getItem("x-auth-token")
    //     this.changeIsLogined(Boolean(mytoken))
    //   }
    // }
  },
  computed:{
    ...mapState({
      isLogined: state => state.loginStatus.isLogined,
      cartTotal: state => state.userInfo.cartTotal,
      userInfo: state => state.userInfo.userInfo
    })
  },
  methods:{
    ...mapMutations({
      changeIsShowLoginModal:"showModal/changeIsShowLoginModal",
      changeIsLogined:"loginStatus/changeIsLogined",
      changeUserInfo:"userInfo/changeUserInfo",
      initUserInfo:"userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeUserInfo:"userInfo/asyncChangeUserInfo"
    })
  }
}
</script>

<style lang="less" scoped>
  .tabbar{
    height: 40px;
    line-height: 40px;
    background: #242B39;
    font-size: 14px;
    color: #FFFEFE;
    .tabbarwrap{
      display: flex;
      justify-content: space-between;
      .right{
        li{
          float: left;
          margin-left: 20px;
          cursor: pointer;
          text-align: center;
          img{
            vertical-align: middle;
            border-radius: 50%;
          }
        }
         .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center; 
        img{
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span{
          margin: 0 8px;
        }
        strong{
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
        .btnlogin{
          padding: 0 15px;
            background: #0A328E;
          }
      }
    }
  }
</style>